Sealed Classes
Sealed classes let you restrict the use of inheritance. Once you declare a class sealed, it can only be subclassed from inside the same package where the sealed class is declared. It cannot be subclassed outside of the package where the sealed class is declared.
sealed class Mammal(val name: String) // 1
class Cat(val catName: String) : Mammal(catName) // 2
class Human(val humanName: String, val job: String) : Mammal(humanName)
fun greetMammal(mammal: Mammal): String {
when (mammal) { // 3
is Human -> return "Hello ${mammal.name}; You're working as a ${mammal.job}" // 4
is Cat -> return "Hello ${mammal.name}" // 5
} // 6
}
fun main() {
println(greetMammal(Cat("Snowy")))
}
- Defines a sealed class.
- Defines subclasses. Note that all subclasses must be in the same package.
- Uses an instance of the sealed class as an argument in a
when
expression. - A smartcast is performed, casting
Mammal
toHuman
. - A smartcast is performed, casting
Mammal
toCat
. - The
else
-case is not necessary here since all possible subclasses of the sealed class are covered. With a non-sealed superclasselse
would be required.